class renwu implements Runnable {
    private static int number = 0;
    private static final Object lock = new Object();
    private final boolean isEven;
    public renwu(boolean isEven) {
        this.isEven = isEven;
    }

    @Override
    public void run() {
        while (true) {
            synchronized (lock) {
                // 当数字超出 200 时，退出循环
                if (number > 200) {
                    break;
                }
                // 检查当前数字的奇偶性是否与该线程应打印的奇偶性一致
                if ((number % 2 == 0) == isEven) {
                    System.out.println(Thread.currentThread().getName() + ": " + number);
                    number++;
                    try {
                        // 模拟处理时间
                        Thread.sleep(10);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    // 唤醒其他等待的线程
                    lock.notify();
                } else {
                    try {
                        // 当前线程等待
                        lock.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
}

public class Test04 {
    public static void main(String[] args) {
        // 创建两个线程，一个打印偶数，一个打印奇数
        Thread evenThread = new Thread(new renwu(true), "偶数");
        Thread oddThread = new Thread(new renwu(false), "奇数");

        try {
            // 启动偶数线程
            evenThread.start();
            // 启动奇数线程
            oddThread.start();

            // 等待偶数线程执行完毕
            evenThread.join();
            // 等待奇数线程执行完毕
            oddThread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}